/*U41ajtHAnBXc4J9azvHa6ediPGD9LUURBy7KttFrrVq4t+wt2umvO59Wch1TMVGv*/
/*
 * @Author: xiahui
 * @PageInfo: 表体编辑前事件
 * @Date: 2019-01-14 14:32:21
 * @Last Modified by: zhangflr
 * @Last Modified time: 2021-09-22 10:20:17
 */
import { PAGECODE, AREA, URL, FIELDS, VERSION, FSTATUSFLAG, BUYSELLFLAG, FREEFIELD, UISTATUS } from '../../constance';
import { bodyReferFilter } from '../referFilter/bodyReferFilter';
import remoteRequest from '../../../pub/beforeEvents/remoteRequest';
import { showWarningInfo } from '../../../../../scmpub/scmpub/pub/tool/messageUtil';
import { marAsstUtils, crossRuleUtils } from '../../../../../scmpub/scmpub/pub/tool';
import { getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';

export default async function(props, moduleId, key, value, index, record) {
	let fixAssts = [ 'casscustvid', 'casscustid', 'cbprojectid', 'cproductorid', 'cvendorvid', 'cvendorid' ];

	let ctrantypeid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.ctrantypeid).value; // 交易类型
	let pk_material = record.values[FIELDS.pk_material]; // 物料
	let editFlag = true; // 编辑状态初始值
	if (FIELDS.pk_material == key) {
		// 物料基本-物料
		editFlag = await materialAndMarbasclass.call(this, moduleId, key, index, ctrantypeid);
		if (editFlag) {
			let nordnum = record.values[FIELDS.nordnum];
			let csrcid = record.values[FIELDS.csrcid];
			// 判断是否生成过订单
			if (nordnum && nordnum.value > 0) {
				showWarningInfo(null, getLangByResId(this, '4004PURDAILY-000002')); /* 国际化处理： 该物料行已生成过订单，不能修改!*/
				return false;
			}
			// 判断是否有来源
			if (csrcid && csrcid.value) return false;

			bodyReferFilter(props, moduleId, key, value, index, record);
		}
	} else if (FIELDS.pk_marbasclass == key) {
		// 物料基本-物料基本分类
		editFlag = materialAndMarbasclass.call(this, moduleId, key, index, ctrantypeid);
	} else if (FIELDS.ctranspmodeid == key || FIELDS.cqualitylevelid == key) {
		// 物料基本-运输方式|质量等级
		bodyReferFilter(props, moduleId, key, value, index, record);
	} else if (FIELDS.vqtunitrate == key) {
		// 报价单位换算率
		let pk_material = record.values[FIELDS.pk_material];
		let cqtunitid = record.values[FIELDS.cqtunitid];
		let cunitid = record.values[FIELDS.cunitid];
		if (!pk_material && !material.value) {
			return false;
		}

		let constance = {
			key: key,
			areaid: moduleId,
			index: index,
			params: {
				pk_material: pk_material.value,
				cqtunitid: cqtunitid.value,
				cunitid: cunitid.value
			}
		};

		editFlag = await remoteRequest(URL.bodyBeforeEdit, constance);
	} else if (FIELDS.vchangerate == key) {
		// 换算率
		let castunitid = record.values[FIELDS.castunitid];
		let cunitid = record.values[FIELDS.cunitid];
		if (!pk_material && !material.value) {
			return false;
		}

		let constance = {
			key: key,
			areaid: moduleId,
			index: index,
			params: {
				pk_material: (pk_material || {}).value,
				castunitid: castunitid.value,
				cunitid: cunitid.value
			}
		};

		editFlag = await remoteRequest(URL.bodyBeforeEdit, constance);
	} else if (FIELDS.castunitid == key || FIELDS.cqtunitid == key) {
		// 单位 | 报价单位
		if (!ctrantypeid || !pk_material || !pk_material.value) {
			return false;
		}

		let constance = {
			key: key,
			areaid: moduleId,
			index: index,
			params: {
				ctrantypeid: ctrantypeid
			}
		};

		editFlag = await remoteRequest(URL.bodyBeforeEdit, constance);
		if (editFlag) {
			bodyReferFilter(props, moduleId, key, value, index, record);
		}
	} else if (FIELDS.nastnum == key) {
		// 数量
		if (!ctrantypeid) return false;
	} else if (FIELDS.vmemo == key && moduleId == AREA.cardChangeId) {
		// 变更历史-备注
		let vchangecode = parseInt(record.values[FIELDS.vchangecode].value); // 变更版本号
		// 变更版本1不可修改
		if (VERSION.v1 == vchangecode) return false;
	} else if (FIELDS.vchgreason == key) {
		// 变更历史-变更原因
		let vchangecode = record.values[FIELDS.vchangecode].value; // 变更版本号
		let version = parseInt(props.form.getFormItemsValue(AREA.cardFormId, FIELDS.version).value); // 版本号
		// 只允许编辑非版本1的当前变更版本的变更原因;
		if (VERSION.v1 == vchangecode || version != vchangecode) return false;
		return true;
	} else if (FIELDS.vexecreason == key) {
		// 执行过程-原因
		let fstatusflag = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.fstatusflag).value; // 合同状态
		if (FSTATUSFLAG.frozen == fstatusflag || FSTATUSFLAG.terminate == fstatusflag) return false;
	} else if (FIELDS.cqpbaseschemeid == key) {
		// 优质优价方案
		bodyReferFilter(props, moduleId, key, value, index, record);
	} else if (FIELDS.ctaxcodeid == key) {
		// 税码
		let ctaxcountryid = record.values[FIELDS.ctaxcountryid].value;
		let fbuysellflag = record.values[FIELDS.fbuysellflag].value;
		if (!ctaxcountryid || !fbuysellflag) return false;
		bodyReferFilter(props, moduleId, key, value, index, record);
	} else if (FIELDS.ncaltaxmny == key) {
		// 计税金额
		let fbuysellflag = record.values[FIELDS.fbuysellflag].value;
		// 国内业务时不可编辑
		if (fbuysellflag == BUYSELLFLAG.national_buy || fbuysellflag == BUYSELLFLAG.national_sell) return false;
	} else if (FIELDS.pk_ct_price == key) {
		// 合同价格信息
		let bbracketorder = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.bbracketorder); // 是否总括订单
		if (bbracketorder && bbracketorder.value) {
			showWarningInfo(null, getLangByResId(this, '4004PURDAILY-000003')); /* 国际化处理： 总括订单不能关联价格信息表*/
			return false;
		}

		let pk_org = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.pk_org).value;
		let cvendorid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.cvendorid).value; // 供应商
		let corigcurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.corigcurrencyid).value; // 原币币种
		let pk_marbasclass = record.values[FIELDS.pk_marbasclass].value; // 物料基本分类
		let pk_material = record.values[FIELDS.pk_srcmaterial].value; // 物料OID
		let cqtunitid = record.values[FIELDS.cqtunitid].value;
		if (!pk_org || !cvendorid || !corigcurrencyid || (!pk_marbasclass && (!pk_material || !cqtunitid))) {
			showWarningInfo(
				null,
				getLangByResId(this, '4004PURDAILY-000004')
			); /* 国际化处理： 匹配维度：采购组织、供应商、物料分类编码或者物料编码、币种、业务单位不能有空*/
			return false;
		}

		bodyReferFilter(props, moduleId, key, value, index, record);
	} else if (FIELDS.effectmonth == key || FIELDS.effectaddmonth == key) {
		// 生效月 | 附加月
		let checkdata = record.values[FIELDS.checkdata].value; // 固定结账日
		if (!checkdata) return false;
	} else if (FIELDS.vtermcode == key) {
		// 条款编码
		// 设置多选
		props.meta.getMeta()[moduleId].items.find((item) => item.attrcode == key).isMultiSelectedEnabled = true;
		props.cardTable.setQueryCondition(moduleId, {
			[key]: () => {
				let pk_org = (props.form.getFormItemsValue(AREA.cardFormId, 'pk_org') || {}).value;
				return { pk_org: pk_org };
			}
		});
	} else if (FIELDS.pk_financeorg_v == key) {
		//结算财务组织
		let status = props.getUrlParam('status');
		if (status == UISTATUS.modify) {
			return false;
		}
	} else if (key.indexOf('vbdef') == 0) {
		let pk_org = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.pk_org).value;
		if (!pk_org) {
			return false;
		}
		// 设置参照所属组织
		bodyReferFilter(props, moduleId, key, value, index, record);
	} else if (key.indexOf('vfree') == 0 || fixAssts.includes(key)) {
		// 物料为空，不可编辑
		if (!pk_material.value) {
			showWarningInfo(null, getLangByResId(this, '4004PURDAILY-000005')); /* 国际化处理： 请先录入物料信息。*/
			return false;
		}
		let flag;
		if (fixAssts.includes(key)) {
			flag = true;
		} else {
			// 后台
			let constance = {
				key: key,
				areaid: moduleId,
				index: index,
				params: {
					pk_material: pk_material.value
				}
			};
			flag = await remoteRequest(URL.bodyBeforeEdit, constance);
		}
		if (flag) {
			// 设置参照所属组织
			marAsstUtils.resetItem.call(this, props, null, PAGECODE.cardPagecode, moduleId, key, record, FREEFIELD);
		}
		editFlag = flag;
	}
	if (editFlag && moduleId == AREA.cardTableId) {
		// 执行交叉校验规则
		let crossRuleParams = {
			props,
			key, //当前字段
			appcode: null, //小应用编码，如果是本应用，可为空
			pagecode: PAGECODE.cardPagecode, //页面编码
			headarea: AREA.cardFormId, //表头区域编码
			bodyarea: AREA.cardTableId, //表体区域编码
			isHead: false, //是否为表头区字段
			record, //当前表体行数据，如果是表头触发，可以为空
			pk_org_field: FIELDS.pk_org, //组织字段，注意为oid
			billtype: 'Z2', //单据类型
			transtypeid_field: FIELDS.ctrantypeid //交易类型id字段
		};

		crossRuleUtils.beforeEdit.call(this, crossRuleParams);
	}
	return editFlag;
}

/**
 * 物料和物料分类公共编辑前
 * @param {*} props
 */
async function materialAndMarbasclass(moduleId, key, index, ctrantypeid) {
	if (!ctrantypeid) {
		showWarningInfo(null, getLangByResId(this, '4004PURDAILY-000006')); /* 国际化处理： 请先选择交易类型*/
		return false;
	}

	let constance = {
		key: key,
		areaid: moduleId,
		index: index,
		params: {
			ctrantypeid: ctrantypeid
		}
	};

	let flag = await remoteRequest(URL.bodyBeforeEdit, constance);
	return flag;
}

/*U41ajtHAnBXc4J9azvHa6ediPGD9LUURBy7KttFrrVq4t+wt2umvO59Wch1TMVGv*/